/**
 * Created by thanatos on 15-9-13.
 */

module.exports = {

    trimspace: function (str) {
        return str.replace(/(^\s+)|(\s+$)/g, '').replace(/\s+/g, ' ')
    },

    //将url的参数封装成对象
    params2obj: function(query){
        var params = {}
        for (var key in query) {
            if (key.indexOf('.') == -1) {
                params[key] = query[key]
            } else {
                key.split('.').reduce(function (pre, cur, index, array) {
                    if (index == 1) {
                        if (!params[pre]) {
                            params[pre] = {}
                        }
                        pre = params[pre]
                    }
                    if (index == array.length - 1) {
                        pre[cur] = pre[cur] ? [].concat(pre[cur], query[key]) : query[key]
                        //pre[cur] = query[key]            //我们使用覆盖，也就是抛弃多选
                    } else {
                        if (!pre[cur])
                            pre[cur] = {}
                        return pre[cur]
                    }
                })
            }
        }
        return params
    },

    merge4params: function () {
        var params = []
        for(var i=0; i<arguments.length; i++){
            var arg = arguments[i]
            if(arg){
                switch(typeof(arg)){
                    case 'string':
                        params.push(arg)
                        break
                    case 'object':
                        for(var key in arg){
                            if(arg[key] && arg[key]!='undefined'){
                                params.push(key+'='+arg[key])
                            }
                        }
                        break
                }
            }
        }
        return params.join('&')
    }


}